package team1.project2.vmlogs;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.apache.log4j.Logger;

public class LogsProcessor {
	
	Logger log = Logger.getLogger(LogsProcessor.class);
	private String	fileType	= null;
	private String	logDir	 = null;
	
	
	public LogsProcessor(String fileType, String logDir) {
		super();
		this.fileType = fileType;
		this.logDir = logDir;
	}
	
	public static void main(String[] args) {
		
//		String line = "2012-10-30T04:16:12Z Unknown: name=Pray_Backup_Pool:_remoteMoRef=vim.ResourcePool:pool20', 'resgroup-720': '_id=resgroup-720:_name=Pooja_Pool:_remoteMoRef=vim.ResourcePool:pool21', 'resgroup-737': '_id=resgroup-737:_name=Stuti_Backup_Pool:_remoteMoRef=vim.ResourcePool:pool22', 'resgroup-8': '_id=resgroup-8:_name=Resources:_remoteMoRef=vim.ResourcePool:ha-root-pool', 'resgroup-929': '_id=resgroup-929:_name=Pooja_Clone_pool:_remoteMoRef=vim.ResourcePool:pool23', 'resgroup-931': '_id=resgroup-931:_name=Krishna_Clone_Pool:_remoteMoRef=vim.ResourcePool:pool24', 'resgroup-934': '_id=resgroup-934:_name=Karthik_Pool:_remoteMoRef=vim.ResourcePool:pool25'}";
//		try {
//			new LineProcessor().parseSysLogLines(line);
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		String kernelLogPath = "C:/logfiles/kernel/";
//		new LogsProcessor(VMLogsConstants.KERNEL_LOGS, kernelLogPath)
//		        .processPath();

		String sysLogPath = "C:\\Documents and Settings\\family.PDPHOME-A54F0A5\\Desktop\\kavya\\New Folder\\";
		new LogsProcessor(VMLogsConstants.KERNEL_WARNINGS, sysLogPath)
		        .processPath();

		// new ParseLine().processPath("C:/logfiles/sys");
		
	}
	
	public void processPath() {
		File folder = new File(logDir);
		File[] listOfFiles = folder.listFiles();
		for (int i = 0; i < listOfFiles.length; i++) {
			if (listOfFiles[i].isFile()) {
				String filePath = logDir + listOfFiles[i].getName();
				System.out.println(filePath);
				processFile(filePath);
			}
		}
	}
	
	public void processFile(String fileName) {
		Scanner scanner = null;
		try {
			scanner = new Scanner(new File(fileName));
		} catch (FileNotFoundException fileNotFoundException) {
			fileNotFoundException.printStackTrace();
			throw new RuntimeException("File not found, filename "+fileName);
		}
		scanner.useDelimiter("\n");
		int success = 0;
		int failure = 0;
		
		LineProcessor parseLine = new LineProcessor();
		List<KernelMessage> kernelMessages = new ArrayList<KernelMessage>();
		List<KernelMessage> kernelwarnings = new ArrayList<KernelMessage>();
		List<SysMessage> hostd = new ArrayList<SysMessage>();
		List<SysMessage> sysMessages = new ArrayList<SysMessage>();
		while (scanner.hasNext()) {
			String line = scanner.next();
			try {
				System.out.println("File type: " + fileType);
				if (VMLogsConstants.KERNEL_LOGS.equals(fileType)) {
					KernelMessage kernelMessage = parseLine.parseKernelLogLines(line);
					kernelMessages.add(kernelMessage);
				} else if (VMLogsConstants.KERNEL_WARNINGS.equals(fileType)) {
					KernelMessage kernelMessage = parseLine.parseKernelLogLines(line);
					kernelwarnings.add(kernelMessage);
				}else if (VMLogsConstants.SYS_LOGS.equals(fileType)) {
					System.out.println("Line is: " + line);
					SysMessage message =  parseLine.parseSysLogLines(line);
					sysMessages.add(message);
				} else {
					System.out.println("Unknown Type");
				}
				success++;
			} catch (ParseException parseException) {
				parseException.printStackTrace();
				failure++;
			} catch (Exception exception) {
				exception.printStackTrace();
				failure++;
			}
		}
		if(kernelMessages.size()>0){
			new LogDataLoader().loadKernelBatch(kernelMessages);
		}
		if(kernelwarnings.size() > 0){
			new LogDataLoader().loadKernelWarnings(kernelwarnings);
		}
		if(sysMessages.size() > 0){
			new LogDataLoader().loadSysBatch(sysMessages);
		}
		
		System.out.println("Success :" + success + " failure : " + failure);
	}
	
}
